<?php 
class Form extends Object
{
	public $Title;
	public $Method;
	public $MethodTxt;
	public $Action;
	public $MailTo;
	public $Columns;
	public $Valid;
	public $HumanCheck;
	public $Params;
	public $Fields;
	
	public function getTitle() { return $this->Title; }
	public function getMethod($numeric=false) 
	{
		if($numeric == false)
		{ return $this->MethodTxt; }
		else
		{ return $this->Method; }
	}
	public function getParam($param='') 
	{ 
		if(isset($this->Params[$param]))
		{ return $this->Params[$param]; } 
	}
	public function getColumns() { return intval($this->Columns); }
	public function getValid() { return $this->Valid; }
	public function getHumanCheck() { return intval($this->HumanCheck); }
	/* Get Fields */ 
	public function getField($key) 
	{ 
		self::loadFields();
		if(isset($this->Fields[$key]))
		{ return $this->Fields[$key]; }
	}
	public function getFields() 
	{ 
		self::loadFields();
		return $this->Fields; 
	}
	public function getCurrentField()
	{ 
		if((!(is_object($this->CurrentField))) && (self::getCurrentFieldId() > 0))
		{ self::loadFields(self::getCurrentFieldId()); }
		return $this->CurrentField; 
	}
	public function getCurrentFieldId() { return $this->CurrentFieldId; }
	/* Get Fields End */
	
	public function setTitle($val) { $this->Title=$val; }
	public function setMethod($val) 
	{ 
		if(is_numeric($val))
		{
			$this->Method=$val;
			if($val > 0)
			{ $this->MethodTxt = 'get'; }
			else
			{ $this->MethodTxt = 'post'; }
		}
		else
		{
			if($val == 'get')
			{
				$this->Method=1;
				$this->MethodTxt = 'get';
			}
			else
			{
				$this->Method=0;
				$this->MethodTxt = 'post';
			}
		}
	}
	public function setParam($param='', $val) 
	{ 
		if($param != '')
		{ $this->Params[$param]=$val; } 
	}
	public function setParams($val) { $this->Params=$val; }
	public function setColumns($val) { $this->Columns=intval($val); }
	public function setValid($val) { $this->Valid=$val; }
	public function setHumanCheck($val) { $this->HumanCheck=intval($val); }
	public function setFields($val) { $this->Fields=$val; }
	
	public function getFieldHTML($fieldKey='', $format='fieldset')
	{
		$out = '';
		if($this->Fields[$fieldKey])
		{ $out .= $this->Fields[$fieldKey]->getHTML($format); }
		return $out;
	}
	
	public function getFieldLabelHTML($fieldKey='', $format='fieldset')
	{
		$out = '';
		if($this->Fields[$fieldKey])
		{ $out .= $this->Fields[$fieldKey]->getLabelHTML($format); }
		return $out;
	}
	
	public function getFieldsHTML($txt='', $format='fieldset')
	{
		$out = '';
		if($this->Fields)
		{
			foreach($this->Fields as $field)
			{ $out .= $field->getHTML($format); }
		}
		if($this->HumanCheck > 0)
		{
			//$out .= 'Capthca';
		}
		return $out;
	}
	
	public function getFormTableHTML()
	{
		$out = '<table class="form-data" border="0" cellspacing="0" cellpadding="0">';
		if(self::getFields())
		{
			$i = 1;
			$cspan = 1;
			$out .= '<tr>';
			foreach(self::getFields() as $field)
			{ 
				$cspan = 1;
				if(stristr($field->getClass(), 'newline'))
				{ $out .= '</tr><tr><td>&nbsp;</td></tr><tr>'; $i = 0; $cspan=self::getColumns(); }
				
				$out .= '<td colspan="'.$cspan.'">'.$field->getHTML('form_table_columns').'</td><td>&nbsp;&nbsp;&nbsp;&nbsp;</td>'; 
				
				if(self::getColumns() > 0)
				{
					if(($i % self::getColumns()) == 0)
					{ $out .= '</tr><tr><td>&nbsp;</td></tr><tr>'; $i = 0; }
				}
				else
				{ $out .= '</tr><tr>'; }
				$i++;
			}
			$out .= '</tr>';
		}
		$out .= '</table>';
		return $out;
	}
	
	public function getFormBsHTML($inline=4)
	{
		$out = '<div class="form-data">';
		if(self::getFields())
		{
			$i = 1;
			$cspan = 1;
			$colClass = 'col-md-3';
			
			$out .= '<div class="row">';
			
			switch(self::getColumns())
			{
				case 0:
				case 1:
					$colClass = 'col-md-12';
				break;
				case 2:
					$colClass = 'col-md-6';
				break;
				case 3:
					$colClass = 'col-md-4';
				break;
				case 4:
					$colClass = 'col-md-3';
				break;
				case 5:
				case 6:
					$colClass = 'col-md-2';
				break;
				case 7:
				case 8:
				case 9:
				case 10:
				case 11:
				case 12:
					$colClass = 'col-md-1';
				break;
			}
			
			foreach(self::getFields() as $field)
			{ 
				$cspan = 1;
				if(stristr($field->getClass(), 'newline'))
				{ $out .= '</div><div class="row"></div><div class="row">'; $i = 0; $cspan=self::getColumns(); }
				
				$out .= '<div class="'.$colClass.'">'.$field->getHTML().'</div>'; 
				
				if(self::getColumns() > 0)
				{
					if(($i % self::getColumns()) == 0)
					{ $out .= '</div><div class="row">'; $i = 0; }
				}
				else
				{ $out .= '</div><div class="row">'; }
				$i++;
			}
			$out .= '</div>';
		}
		$out .= '</div>';
		return $out;
	}
	
	public function getFieldsDataHTML($format='table_lines')
	{
		$out = '';
		if($format == 'table_lines')
		{ $out = '<table class="form-data" border="0" cellspacing="0" cellpadding="0">'; } 
		
		if($this->Fields)
		{
			foreach($this->Fields as $field)
			{ $out .= $field->getDataHTML($format); }
		}
		if($format == 'table_lines')
		{ $out .= '</table>'; } 
		return $out;
	}
	
	public function getFieldsParamArray() 
	{ 
		if(self::getFields())
		{
			foreach(self::getFields() as $field)
			{ 
				switch($field->getType())
				{
					case 'select':
					case 'radio':
					case 'checkbox':
					case 'textarea':
					case 'text':
					case 'password':
					case 'hidden':
						$fields[$field->getName()] = $field->getValue();
					break;
				}
			}
			if(count($fields) > 0)
			{ return $fields; }
		}
	}
	
	public function validate($txt='', $com='')
	{
		if(self::getFields())
		{
			self::setValid(1);
			foreach(self::getFields() as $field)
			{
				$field->validate();
				if($field->getValid() == 0)		
				{ self::setValid(0); }
			}
		}
		return self::getValid();
	}
	
	function update()
	{
		if((self::getId() > 0) && (self::getDataChanged()))
		{
			$sql  = 'UPDATE '.DB_PERFIX.'fm_forms SET ';
			$sql .= '`title`="'.self::formatText(self::getTitle()).'", ';
			$sql .= '`method`='.self::getMethod(true).', ';
			$sql .= '`action`="'.self::formatUrl(self::getAction()).'", ';
			$sql .= '`MailTo`="'.self::formatText(self::getMailTo()).'", ';
			$sql .= '`humanCheck`='.self::getHumanCheck().' ';
			$sql .= ' WHERE id='.self::getId().' LIMIT 1;';
			mysql_query($sql);
		}
	}
	
	public function addFormField($name='', $label = '', $type=0)
	{
		$name 	= self::formatText($name);
		$label 	= self::formatText($label);
		
		if((self::getId() > 0) && ($name != ''))
		{
			$neworder = 0;
			$result = mysql_query('SELECT fieldOrder FROM '.DB_PERFIX.'fm_form_fields WHERE formId='.self::getId().' ORDER BY fieldOrder DESC LIMIT 1;');
			if(mysql_num_rows($result) > 0)
			{
				while($row = mysql_fetch_array($result))
				{ 
					$neworder = $row['fieldOrder'];
					$neworder++;
				}
			}
			$sql =  'INSERT INTO '.DB_PERFIX.'fm_form_fields (';
			$sql .= '`formId`, ';
			$sql .= '`name`, ';
			$sql .= '`label`, ';
			$sql .= '`type`, ';
			$sql .= '`fieldOrder` ';
			$sql .= ') ';
			$sql .= ' VALUES (';
			$sql .= self::getId().', ';
			$sql .= '"'.$name.'", ';
			$sql .= '"'.$label.'", ';
			$sql .= $type.', ';
			$sql .= $neworder.'';
			$sql .= ');';
			
			echo $sql;
			mysql_query($sql);
		}
	}
	
	function loadFields($id=0)
	{
		if(!(self::getItemLoaded('Fields'.$id)))
		{
			if(self::getId() > 0)
			{
				global $db;
				$sql = 'SELECT * FROM '.DB_PERFIX.'fm_form_fields WHERE formId='.self::getId().' ORDER BY fieldOrder ASC;';
				$result = $db->query($sql);
				$data = $db->fetch($sql, CACHE_TTL);
				if(is_array($data))
				{
					foreach($data as $row)
					{
						//($name='', $ty='', $lbl='', $show=true, $req=false, $nospace=false, $validt='anum', $mnl=0, $mxl=0, $comp='', $val='', $params=Array()) 
						$this->Fields[$row['id']] = new FormField($row['name'], $row['type']);
						$this->Fields[$row['id']]->setId($row['id']);
						$this->Fields[$row['id']]->setFormId(self::getId());
						$this->Fields[$row['id']]->setLabel($row['label']);
						$this->Fields[$row['id']]->setInfo($row['info']);
						$this->Fields[$row['id']]->setType($row['type']);
						if(!$_REQUEST[$row['name']])
						{ $this->Fields[$row['id']]->setValue($row['value']); }
						$this->Fields[$row['id']]->setMinLen($row['minLen']);
						$this->Fields[$row['id']]->setMaxLen($row['maxLen']);
						$this->Fields[$row['id']]->setVisible($row['visible']);
						$this->Fields[$row['id']]->setReq($row['req']);
						$this->Fields[$row['id']]->setNospace($row['nospace']);
						$this->Fields[$row['id']]->setValidType($row['validType']);
						$this->Fields[$row['id']]->setCompare($row['compare']);
						$this->Fields[$row['id']]->setSrcTable($row['srcTable']);
						$this->Fields[$row['id']]->setSrcLblSelect($row['srcLblSelect']);
						$this->Fields[$row['id']]->setSrcValSelect($row['srcValSelect']);
						$this->Fields[$row['id']]->setSrcWhere($row['srcWhere']);
						$this->Fields[$row['id']]->setSrcValDef($row['srcValDef']);
						$this->Fields[$row['id']]->setClass($row['class']);
						$this->Fields[$row['id']]->setFieldOrder($row['fieldOrder']);
						$this->Fields[$row['id']]->setActive($row['active']);
					}
				}
				unset($data);
				self::setItemLoaded('Fields'.$id, true);
			}
		}
	}
	
	function deleteFormField($id)
	{	
		if($id > 0)
		{	
			$sql = 'DELETE FROM '.DB_PERFIX.'fm_form_fields WHERE formId='.self::getId().' AND id='.$id.';';
			$result = mysql_query($sql);
			return;
		}
	}
	
	function sendResponseMail()
	{
		global $katana;
		global $txt;
		
		$fontstyle = 'font-family:Arial, Helvetica, sans-serif; font-size:12px;';
		$fontstyleHeading = 'font-family:Arial, Helvetica, sans-serif; font-size:13px;';
		
		$body .= '<body style="'.$fontstyle.'">';
		$body .= 	'<h3 style="'.$fontstyleHeading.'">'.self::getTitle().'</h3>';
	
		if(self::getFields())
		{
			foreach(self::getFields() as $field)
			{
				if($field->getType(true) < 4)
				{
					$body .= '<p style="'.$fontstyle.'">';
					$body .= '<strong>'.ucfirst($txt->getWord($field->getLabel())).': </strong> ';
					$body .= $field->getValue().' ';
					$body .= '</p>';
				}
			}
		}
		
		$body .= '</body>';
		
		
		$mail = new Email($katana);
		if(self::getMailTo() != '')
		{ $mail->setTo(self::getMailTo()); }
		else
		{ $mail->setTo($katana->getCompanyEmail()); }
		//$mail->setTo('anelmedon@gmail.com');
		$mail->setFrom($katana->getCompanyEmail());
		$mail->setFromName($katana->getCompanyName());
		$mail->setSubject($this->getTitle().' '.ucfirst($txt->getWord('response')));
		$mail->setBody($body);
		$mail->send();
	}
}
?>